// pages/user/index.js
var url = getApp().globalData.ApiUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar: 'https://www.zhuisushangcheng.com/data/upload/applet/avatar.png',
    nickName: '未登录',
    user: '',
    coupounsNum: '', //未使用优惠券数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  //点击头像跳转登录
  toLogin: function (){
    var that = this;
    if (!that.data.user){
      wx.navigateTo({
        url: '/pages/authorize/index',
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var key = wx.getStorageSync('key');
    if (key) {
      var userInfo = wx.getStorageSync('userInfo');
      this.setData({
        avatar: userInfo.avatarUrl,
        nickName: userInfo.nickName,
        user: userInfo,
      });
      this.getList('unused');
    }    
  },
  /**
   * 获取有几张优惠券
   */
  getList: function (state) {
    var that = this;
    var key = wx.getStorageSync("key");
    wx.request({
      url: url + '/mobile/index.php?act=member_redpacket&op=redpacket_list',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'POST',
      data: {
        key: key,
        rp_state: state
      },
      success: (res) => {
        if(res.data.code == 200){
          var redpacket_list = res.data.datas.redpacket_list;
          if (redpacket_list.length == 0) {
            that.setData({
              coupounsNum: 0,
            });
          } else {
            that.setData({
              coupounsNum: redpacket_list.length,
            });
          }
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }      
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  showTip: function (){
    wx.reLaunch({
      url: '/pages/authorize/index',
    })
  },
  /**
  *跳转到订单列表
  */
  toOrderList: function (){
    var that = this;
    var key = wx.getStorageSync('key');
    if(key){
      wx.navigateTo({
        url: '/pages/order/order-list/index?key=' + key,
      });
    }else{
      that.showTip();
    }    
  },
  // 跳转我的优惠券页
  toConpounsList: function (){
    var that = this;
    var key = wx.getStorageSync('key');
    if(key){
      wx.navigateTo({
        url: '/pages/mycoupons/index?rp_state=unused&id=0',
      });
    }else{
      that.showTip();
    }
    
  },
  // 跳转到收货地址
  toReceiveAddr: function (){
    var that = this;
    var key = wx.getStorageSync('key');
    if(key){
      wx.navigateTo({
        url: '/pages/about-address/address/index?key=' + key,
      });
    }else{
      that.showTip();
    }    
  },
  // 跳转到待支付页面
  toBePaid: function (){
    var that = this;
    var key = wx.getStorageSync('key');
    if (key) {
      wx.navigateTo({
        url: '/pages/order/be-paid/index?key=' + key,
      });
    } else {
      that.showTip();
    }
  },
  // 跳转到待收货页面
  toBeReceive: function (){
    var that = this;
    var key = wx.getStorageSync('key');
    if (key) {
      wx.navigateTo({
        url: '/pages/order/be-receive/index?key=' + key,
      })
    } else {
      that.showTip();
    }
  }
})